package flyweight.factory;

import flyweight.Message;
import flyweight.impl.EmailMessage;
import flyweight.impl.PhoneMessage;

import java.util.HashMap;

public class MessageFactory {
    private static final HashMap<String, Message> table = new HashMap<>();

    //初始化
    static {
        table.put("phone", new PhoneMessage());
        table.put("email", new EmailMessage());
    }

    public static Message getMessage(String template) throws IllegalArgumentException {
        if (!table.containsKey(template)) {
            throw new IllegalArgumentException("找不到名称为：" + template + "的模板");
        }
        return table.get(template);
    }

    public static void addMessageTemplate(String key, Message message) {
        table.put(key, message);
    }

}
